package com.ym.pms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.pms.dao.BuildingMapper;
import com.ym.pms.dao.ItemMapper;
import com.ym.pms.dao.SuiteMapper;
import com.ym.pms.getCoding.NewCoding;
import com.ym.pms.model.Building;
import com.ym.pms.service.BuildingService;
import com.ym.pms.service.SuiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class BuildingServiceImpl implements BuildingService {

    @Autowired
    BuildingMapper buDao;
    @Autowired
    ItemMapper itDao;
    @Autowired
    SuiteMapper suDao;
    @Autowired
    NewCoding newCoding;
    @Autowired
    SuiteService suiteService;

    @Override
    public int addBu(Building building) {
        building.setBuCoding(getCoding());
        System.out.println(building.getBuCoding());
        building.setBuRegion(itDao.selectByPrimaryKey(building.getBuItid()).getItRegion());
        return buDao.insertSelective(building);
    }

    @Override
    public int deleteBu(String buCoding) {
        suDao.deleteAllByBuCoding(buCoding);
        return buDao.deleteByPrimaryKey(buCoding);
    }

    @Override
    public int updateBu(Building building) {
        if (building.getBuCoding() == null) {
            return -1;//未填写Coding
        } else if (buDao.selectByPrimaryKey(building.getBuCoding()) == null) {
            return 0;//找不到该编码的楼栋
        } else {
            return buDao.updateByPrimaryKeySelective(building);
        }
    }

    @Override
    public PageInfo findAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Building> buildings = buDao.queryBuildingList();
        for (Building building : buildings) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(building.getBuSdate());
            double startYear = calendar.get(Calendar.YEAR);
            double endYear = startYear + building.getBuSlife();
            Calendar calendar2 = Calendar.getInstance();
            calendar2.setTime(new Date());
            double nowYear = calendar2.get(Calendar.YEAR);
            building.setBuRyears(endYear - nowYear);
        }
        PageInfo pageInfo = new PageInfo<Building>(buildings);
        return pageInfo;
    }

    @Override
    public PageInfo displayInformation(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Building> buildings = buDao.selectOfDisplay();
        PageInfo pageInfo = new PageInfo<Building>(buildings);
        return pageInfo;
    }

    @Override
    public Building findByCoding(String bu_Coding) {
        Building building = buDao.selectByPrimaryKey(bu_Coding);
        return building;
    }

    @Override
    public PageInfo fuzzyQuery(Building building,int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Building> buildings = buDao.fuzzyQuery(building);
        PageInfo pageInfo = new PageInfo<Building>(buildings);
        return pageInfo;
    }

    @Override
    public int getMaxNo() {
        Integer BuildingMaxNo = buDao.getBuildingMaxNo();
        if (BuildingMaxNo == null) {
            return 0;
        } else {
            return BuildingMaxNo;
        }
    }

    @Override
    public String getCoding() {
        Integer maxNo = this.getMaxNo();
        maxNo += 1;
        String BuCoding = newCoding.getCoding("BU", maxNo);
        return BuCoding;
    }

    @Override
    public void deleteAllByItId(String id) {
        List<String> allCoding = buDao.getAllCoding(id);
        for (String s : allCoding) {
            suiteService.deleteAllByBuCoding(s);
        }
        buDao.deleteAllByItId(id);
    }
}
